package com.example.test.student;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.test.R;
import com.example.test.utils.Http;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class GradeActivity extends AppCompatActivity {
    private EditText gradeName;
    private Button getGrade;
    private ListView listView;
    private List<Map<String, Object>> ListViewData;
    private String[] lesson = new String[99];
    private int[] grade = new int[99];
    private int length = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grade_student);
        init();
        getUrl("");
    }

    public void init()
    {
        listView = findViewById(R.id.grade_listView);
        gradeName = findViewById(R.id.editText);
        getGrade = findViewById(R.id.button);
        getGrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getUrl(gradeName.getText().toString());
            }
        });
    }

    public void setListView()
    {
        ListViewData=new ArrayList<>();
        for(int i=0;i<length;i++)
        {
            Map<String, Object> map= new HashMap<>();
            map.put("lesson", lesson[i]);
            map.put("grade",grade[i]);
            ListViewData.add(map);
        }
        SimpleAdapter adapter=new SimpleAdapter(this, ListViewData, R.layout.student_listview_grade, new String[]{"lesson","grade"}, new int[]{R.id.lesson_grade,R.id.grade});
        listView.setAdapter(adapter);
    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);  // 获取item高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        // 最后再加上分割线的高度和padding高度，否则显示不完整。
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1))+listView.getPaddingTop()+listView.getPaddingBottom();
        listView.setLayoutParams(params);
    }

    public void getUrl(String name) {
        final String name1 = name;
        new Thread(new Runnable() {
            @Override
            public void run() {
                StringBuilder url = new StringBuilder();

                String res;
                if (name1.equals(""))
                    res = Http.sendGet("http://36428pn632.qicp.vip/student/index.php/role/role/index?action=class");
                else
                    res = Http.sendGet("http://36428pn632.qicp.vip/student/index.php/role/role/index?name="+name1);
                //                            object = new JSONObject(res);
                final LinkedHashMap<String, Object> hashMaps = Http.stringToJsonObject(res);
                Log.d("HttpGET", (String) hashMaps.get("msg"));
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d("data", "数据处理开始");
                        Map result =(Map) hashMaps.get("result");
                        Log.d("HttpGET", ""+result.size());
                        length = result.size();
                        for (int i = 0; i<result.size(); i++) {
                            Map item = (Map) result.get(""+i);
                            lesson[i] = item.get("class_name").toString();
                            grade[i] = (int)item.get("class_garle");
                        }
                        Log.d("data", "数据处理结束");
                        setListView();
                        setListViewHeightBasedOnChildren(listView);
                    }
                });
            }
        }).start();
    }
}
